/*
 * PastaAddFileBox.java
 */

package pasta;

import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
import org.jdesktop.application.Action;

public class PastaAddFileBox extends javax.swing.JDialog {
    //private final PastaFileChooserBox fileChooserBox;

    public PastaAddFileBox(java.awt.Frame parent) {
        super(parent);
        initComponents();
        getRootPane().setDefaultButton(addButton);

        //fileChooserBox = new JFileChooser();
    }

    @Action public void closeAddFileBox() {
        dispose();
    }

    @Action
    public void addFile() throws IOException {
        try {
          // What to do with the file, e.g. display it in a TextArea
         // System.out.println(selectedFile.getAbsolutePath());
          PastaApp.getApplication().getPastApp().insertMusicIntoPast(titleTextField.getText(), artistTextField.getText(), albumTextField.getText(), genderTextField.getText(), selectedFile.getAbsolutePath());

        } catch (IOException ex) {
            System.out.println("Error Inserting Music");
        }
        dispose();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        musicPanel = new javax.swing.JPanel();
        titleLabel = new javax.swing.JLabel();
        titleTextField = new javax.swing.JTextField();
        artistLabel = new javax.swing.JLabel();
        artistTextField = new javax.swing.JTextField();
        albumLabel = new javax.swing.JLabel();
        albumTextField = new javax.swing.JTextField();
        genderLabel = new javax.swing.JLabel();
        genderTextField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(pasta.PastaApp.class).getContext().getResourceMap(PastaAddFileBox.class);
        setTitle(resourceMap.getString("title")); // NOI18N
        setModal(true);
        setName("aboutBox"); // NOI18N
        setResizable(false);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(pasta.PastaApp.class).getContext().getActionMap(PastaAddFileBox.class, this);
        addButton.setAction(actionMap.get("addFile")); // NOI18N
        addButton.setText(resourceMap.getString("addButton.text")); // NOI18N
        addButton.setName("addButton"); // NOI18N

        cancelButton.setAction(actionMap.get("closeAddFileBox")); // NOI18N
        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N

        musicPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("musicPanel.border.title"))); // NOI18N
        musicPanel.setName("musicPanel"); // NOI18N
        musicPanel.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                musicPanelFocusGained(evt);
            }
        });

        titleLabel.setText(resourceMap.getString("titleLabel.text")); // NOI18N
        titleLabel.setName("titleLabel"); // NOI18N

        titleTextField.setText(resourceMap.getString("titleTextField.text")); // NOI18N
        titleTextField.setName("titleTextField"); // NOI18N
        titleTextField.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                titleTextFieldMouseClicked(evt);
            }
        });
        titleTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                titleTextFieldActionPerformed(evt);
            }
        });
        titleTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                titleTextFieldFocusGained(evt);
            }
        });

        artistLabel.setText(resourceMap.getString("artistLabel.text")); // NOI18N
        artistLabel.setName("artistLabel"); // NOI18N

        artistTextField.setText(resourceMap.getString("artistTextField.text")); // NOI18N
        artistTextField.setName("artistTextField"); // NOI18N

        albumLabel.setText(resourceMap.getString("albumLabel.text")); // NOI18N
        albumLabel.setName("albumLabel"); // NOI18N

        albumTextField.setText(resourceMap.getString("albumTextField.text")); // NOI18N
        albumTextField.setName("albumTextField"); // NOI18N

        genderLabel.setText(resourceMap.getString("genderLabel.text")); // NOI18N
        genderLabel.setName("genderLabel"); // NOI18N

        genderTextField.setText(resourceMap.getString("genderTextField.text")); // NOI18N
        genderTextField.setName("genderTextField"); // NOI18N

        org.jdesktop.layout.GroupLayout musicPanelLayout = new org.jdesktop.layout.GroupLayout(musicPanel);
        musicPanel.setLayout(musicPanelLayout);
        musicPanelLayout.setHorizontalGroup(
            musicPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(musicPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(musicPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, genderLabel)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, albumLabel)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, artistLabel)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, titleLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(musicPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(musicPanelLayout.createSequentialGroup()
                        .add(genderTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 162, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .add(musicPanelLayout.createSequentialGroup()
                        .add(musicPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(albumTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 291, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(artistTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)
                            .add(titleTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE))
                        .add(78, 78, 78))))
        );
        musicPanelLayout.setVerticalGroup(
            musicPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(musicPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(musicPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(titleTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(titleLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(musicPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(artistTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(artistLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(musicPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(albumTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(albumLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(musicPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(genderTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(genderLabel))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(addButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cancelButton))
                    .add(musicPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 381, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(43, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(musicPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addButton)
                    .add(cancelButton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void titleTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_titleTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_titleTextFieldActionPerformed

    private void musicPanelFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_musicPanelFocusGained
        // TODO add your handling code here:
    }//GEN-LAST:event_musicPanelFocusGained

    private void titleTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_titleTextFieldFocusGained
        // TODO add your handling code here:
    }//GEN-LAST:event_titleTextFieldFocusGained

    private void titleTextFieldMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_titleTextFieldMouseClicked
        // TODO add your handling code here:
        fileChooser = new JFileChooser();
        int returnVal = fileChooser.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            selectedFile = fileChooser.getSelectedFile();
            titleTextField.setText(selectedFile.getName());
        }
    }//GEN-LAST:event_titleTextFieldMouseClicked
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JLabel albumLabel;
    private javax.swing.JTextField albumTextField;
    private javax.swing.JLabel artistLabel;
    private javax.swing.JTextField artistTextField;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel genderLabel;
    private javax.swing.JTextField genderTextField;
    private javax.swing.JPanel musicPanel;
    private javax.swing.JLabel titleLabel;
    private javax.swing.JTextField titleTextField;
    // End of variables declaration//GEN-END:variables

    private javax.swing.JFileChooser fileChooser;
    private File selectedFile;

}
